package admin

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"strings"
	. "xm_shop/database"
	. "xm_shop/models"
)

type GoodsColorController struct{}

// 获取商品的所有颜色
func (g GoodsCateController) GetOnlyGoodsColor(context *gin.Context) {
	goodsID, err1 := strconv.Atoi(context.Param("id"))
	if err1 != nil {
		context.String(http.StatusBadRequest, "参数有误")
		return
	}
	// 检索商品
	var goods Goods
	DB.Where("id = ?", goodsID).Take(&goods)
	// 切片
	goodsColorSlice := strings.Split(goods.GoodsColor, ",")
	// 创建颜色map
	goodsColorMap := make(map[string]string)
	for _, v := range goodsColorSlice {
		goodsColorMap[v] = v // ID映射
	}
	// 检索所有颜色
	var colorList []GoodsColor
	DB.Find(&colorList)
	for i := 0; i < len(colorList); i++ {
		IdStr := strconv.Itoa(colorList[i].ID)
		if _, ok := goodsColorMap[IdStr]; ok {
			colorList[i].Checked = true
		}
	}
	context.JSON(http.StatusOK, gin.H{
		"goodsColorList": colorList,
	})
}
